﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Threading;

namespace YeaTrip.Comet
{
    public class CometAsyncResult : IAsyncResult
    {
        private HttpContext context;
        private AsyncCallback callback;
        private object asyncState;
        private bool isCompleted = false;
        private object responseObject;
        private DateTime beginTime;
        
        internal string clientID;
        //internal AgentState agentState;

        public CometAsyncResult(HttpContext context, AsyncCallback callback, object asyncState)
        {
            this.callback = callback;
            this.context = context;
            this.asyncState = asyncState;
            this.beginTime = DateTime.Now;

            this.clientID = context.Request["CustID"].Trim();
            //this.agentState = (AgentState) int.Parse(context.Request["AgentState"].Trim());
        }

        #region IAsyncResult Members

        public object AsyncState
        {
            get { return this.asyncState; }
        }

        public WaitHandle AsyncWaitHandle
        {
            get { throw new InvalidOperationException("ASP.NET Should never use this property"); }
        }

        public bool CompletedSynchronously
        {
            get { return false; }
        }

        public bool IsCompleted
        {
            get { return this.isCompleted; }
        }

        #endregion

        public HttpContext HttpContext
        {
            get { return this.context; }
        }

        public object ResponseObject
        {
            get { return this.responseObject; }
            set { this.responseObject = value; }
        }

        public DateTime BeginTime
        {
            get
            {
                return this.beginTime;
            }
        }


        public void BeginWaitRequest()
        {
            CometThreadPool.QueueCometWaitRequest(this);
        }

        internal void SetCompleted()
        {
            this.isCompleted = true;

            if (callback != null)
                callback(this);
        }
    }
}
